//
//  BeeGroupInviteController.m
//  FbeeAPP
//
//  Created by dev-m on 2018/3/16.
//  Copyright © 2018年 mohao. All rights reserved.
//

#import "BeeGroupInviteController.h"
#import "FbeeInputView.h"
#import "FbeeCommitBtn.h"
#import "BeeGroupListObj.h"

@interface BeeGroupInviteController ()
{
    BeeGroupListObj *beeObj;
    
    UIScrollView *mainView;
    UIButton *backBtn;
    FbeeInputView *inviteCodeF;
    FbeeCommitBtn *commit;
}

@end

@implementation BeeGroupInviteController

-(instancetype)initWithBeeObj:(BeeGroupListObj *)obj
{
    self = [super init];
    
    if (self) {
        beeObj = obj;
    }

    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationView.hidden = YES;
    
    // 添加观察
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
    
    [self buildView];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)buildView
{
    
    UILabel *infoL = [UILabel makeLabel:^(TTLabelExtend *make) {
        make.addFont([UIFont systemFontOfSize:14.0f]).addTextColor(FBEE_COLOR_INFO).addText(NSLocalizedString(@"获得额外奖励", nil));
        make.setFrame(CGRectMake(18, self.largeTitle.bottom + 5, IPHONEWIDTH - 18*2, 16));
        [self.view addSubview:make];
    }];
    
    mainView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, IPHONEWIDTH, IPHONEHEIGHT)];
    mainView.backgroundColor = [UIColor clearColor];
    [self.view addSubview:mainView];
    
    inviteCodeF = [[FbeeInputView alloc] initWithText:NSLocalizedString(@"tNavBeeGroupInviteCode", nil)];
    inviteCodeF.centerX = IPHONEWIDTH/2;
    inviteCodeF.top = 238.5;
    [mainView addSubview:inviteCodeF];
    
    commit = [[FbeeCommitBtn alloc] initWithTitle:NSLocalizedString(@"mConfirm", nil)];
    commit.centerX = IPHONEWIDTH/2;
    commit.top = inviteCodeF.bottom + 65;
    @weakify(self)
    [commit onClick:^{
        @strongify(self)
        [self inputInviteCode];
    }];
    [mainView addSubview:commit];
    
    
    [self.largeTitle setTitle:NSLocalizedString(@"tNavBeeGroupInviteCode", nil)];
    
    backBtn = [UIButton makeButton:^(TTButtonExtend *make) {
        make.setFrame(CGRectMake(0, StatusHeight, 60, 44));
        make.addImage([UIImage imageNamed:@"nav_back"],UIControlStateNormal);
        make.addTarget(self,@selector(navback));
        [self.view addSubview:make];
    }];
    backBtn.imageEdgeInsets = UIEdgeInsetsMake(13.5, 16, 13.5, 27);
    
    @weakify(inviteCodeF);
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithActionBlock:^(id sender) {
        @strongify(inviteCodeF);
        [inviteCodeF resignFirstResponder];
    }];
    [mainView addGestureRecognizer:tap];
    
    [self bringTitleToFront];
    
    if ([KeyBoardManager sharedManager].keyBoardShow) {
        float sub = commit.bottom - (IPHONEHEIGHT - [KeyBoardManager sharedManager].keyHeight);
        if (sub > 0) {
            [mainView setContentOffset:CGPointMake(0, sub) animated:YES];
        }
    }
}

#pragma mark - key

- (void)keyboardWillShow:(NSNotification *)notification {
    float sub = commit.bottom - (IPHONEHEIGHT - [KeyBoardManager sharedManager].keyHeight);
    if (sub > 0) {
        [mainView setContentOffset:CGPointMake(0, sub) animated:YES];
    }
}

- (void)keyboardWillHide:(NSNotification *)notification {
    if (mainView.contentOffset.y>0) {
        [mainView setContentOffset:CGPointMake(0, 0) animated:YES];
    }
}

#pragma mark - requst

-(void)inputInviteCode
{
    if ([NSString isEmptyString:[inviteCodeF getText]]) {
        return;
    }
    
    [self post:BeeGroupInviteCode_URL param:@{@"gid":beeObj.groupId,@"uid":[UserInfoManager sharedManager].userId,@"invitation":[inviteCodeF getText]} code:BeeGroupInviteCode_URL_Code];
    [LoadingHelper showLoadingOnView:self.view];
}

#pragma mark - response

-(void)requestSuccess:(id)data code:(NSInteger)code
{
    [LoadingHelper removeLoadingOnView:self.view];
    [NoticePopHelper showNoticeText:SafeGetStringValue([data objectForKey:@"msg"])];
    beeObj.usedInvitation = [inviteCodeF getText];
    [self.navigationController popViewControllerAnimated:YES];
}

-(void)requestLogicError:(id)data code:(NSInteger)code
{
    [super requestLogicError:data code:code];
    
    [LoadingHelper removeLoadingOnView:self.view];
}

-(void)requestFailure:(NSError *)error code:(NSInteger)code
{
    [LoadingHelper removeLoadingOnView:self.view];
}



@end
